﻿using IBLL;
using IDAL;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class ProductTypeBLL : IProductTypeBLL
    {
        private IProductTypeDAL _productTypeDAL;
        private UnmanContexts _unmanContexts;

        public ProductTypeBLL(
            IProductTypeDAL productTypeDAL
            , UnmanContexts unmanContexts
            )
        {
            _productTypeDAL = productTypeDAL;
            _unmanContexts = unmanContexts;
        }


        #region 添加(CreateProductType)
        public bool CreateProductType(ProductType entity, out string msg)
        {
            //先判断是否非空
            if (string.IsNullOrWhiteSpace(entity.Product_TypeName))
            {
                msg = "类别名称不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(entity.Description))
            {
                msg = "描述不能为空";
                return false;
            }

            //判断类别名是否重复
            ProductType productType = _productTypeDAL.GetEntities().FirstOrDefault(p => p.Product_TypeName == entity.Product_TypeName);
            if (productType != null)
            {
                msg = "类别名已存在";
                return false;
            }
            entity.Id = Guid.NewGuid().ToString();
            entity.CreateTime = DateTime.Now;

            //更新到数据库
            bool isSuccess = _productTypeDAL.CreateEntity(entity);

            msg = isSuccess ? $"添加{entity.Product_TypeName}成功" : "添加类别失败";
            return isSuccess;


        }
        #endregion

        #region 删除(DeleteProductType)
        public bool DeleteProductType(string id)
        {
            //根据id查询用户是否存在
            ProductType productType = _productTypeDAL.GetEntityById(id);
            if (productType == null)
            {
                return false;
            }
            //修改用户的状态
            productType.IsDalete = true;
            productType.DeleteTime = DateTime.Now;
            //返回结果
            return _productTypeDAL.UpdateEntity(productType);
        }
        #endregion

        #region 批量删除(DeleteProductTypes)
        public bool DeleteProductTypes(List<string> ids)
        {
            foreach (var id in ids)
            {
                //根据id查询
                ProductType productType = _productTypeDAL.GetEntityById(id);
                if (productType == null)
                {
                    continue;//跳出本次循环
                }
                productType.IsDalete = true;
                productType.DeleteTime = DateTime.Now;

                _productTypeDAL.UpdateEntity(productType);
            }
            return true;
        }
        #endregion

        #region 查询商品类别表的所有数据(GetProductType)
        public List<GetProductTypeDTO> GetProductTypeList(int page, int limit, string TypeName,  out int count)
        {
            //linq方法进行链表
            var data =_productTypeDAL.GetEntities().Select(p=> new GetProductTypeDTO()
                       {
                           Id = p.Id,
                           Product_TypeName = p.Product_TypeName,
                           Description = p.Description,
                           CreateTime = p.CreateTime,
                       });

            //商品类别不能为空
            if (!string.IsNullOrWhiteSpace(TypeName))
            {
                data = data.Where(p => p.Product_TypeName.Contains(TypeName));
            }
            //总数
            count = data.Count();
            //分页
            var listPage = data.OrderByDescending(p => p.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();
            //返回
            return listPage;
        }
        #endregion


        #region 查询商品类别表
        public List<GetProductTypeDTO> GetProductType()
        {
            List<GetProductTypeDTO> productType = _productTypeDAL.GetEntities()
                                                                 .Where(p => p.IsDalete == false)
                                                                 .Select(p => new GetProductTypeDTO()
                                                                 {
                                                                     Id = p.Id,
                                                                     Product_TypeName = p.Product_TypeName,
                                                                     Description = p.Description,
                                                                     CreateTime = p.CreateTime,
                                                                 }).ToList();

            return productType;
        }
        #endregion

        #region 根据id获取信息的方法(GetProductTypeById)
        public GetProductTypeDTO GetProductTypeById(string id)
        {
            ProductType productType = _productTypeDAL.GetEntityById(id);//先根据id查用户
            if (productType == null)
            {
                return new GetProductTypeDTO();
            }
            //构建返回数据对象
            GetProductTypeDTO category = new GetProductTypeDTO()
            {
                Id = productType.Id,
                Product_TypeName = productType.Product_TypeName,
                Description = productType.Description,
            };

            return category;//返回用户信息
        }
        #endregion


        #region 更新(UpdateProductType)
        public bool UpdateProductType(ProductType productType, out string msg)
        {
            if (string.IsNullOrWhiteSpace(productType.Id))
            {
                msg = "id不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(productType.Product_TypeName))
            {
                msg = "商品类别不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(productType.Description))
            {
                msg = "描述不能为空";
                return false;
            }

            //判断这个id是否存在
            ProductType entity = _productTypeDAL.GetEntityById(productType.Id);
            if (entity == null)
            {
                msg = "id无效";
                return false;
            }
            //先判断是不是和原来的商品类别一样，一样表示没有修改
            if (entity.Product_TypeName != entity.Product_TypeName)
            {
                //同商品类别查询数据库里是否存在相同的商品类别名
                ProductType oldUser = _productTypeDAL.GetEntities().FirstOrDefault(p => p.Description == productType.Description);
                if (oldUser != null)
                {
                    msg = "已被使用";
                    return false;
                }
            }
            //赋值
            entity.Product_TypeName = productType.Product_TypeName;
            entity.Description = productType.Description;

            bool isOk = _productTypeDAL.UpdateEntity(entity);
            msg = isOk ? "修改成功" : "修改失败";
            //返回结果
            return isOk;

        }
        #endregion

    }
}
